package com.coocaa.x.app.hall.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;

import com.coocaa.x.app.hall.parser.ViewParser;
import com.coocaa.x.app.hall.view.background.BackgroundComponent;
import com.coocaa.x.app.hall.view.background.BackgroundFactory;
import com.coocaa.x.app.hall.view.corner.CornerComponent;
import com.coocaa.x.app.hall.view.corner.CornerFactory;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.BaseView;

import java.util.List;

/**
 * Created by luwei on 15-6-16.
 */
public class HallItemView extends BaseView {

    String title = "";
    HallItemView hallItemView = null;
    CornerComponent tipComponet = null;
    BackgroundComponent bgComponet = null;
    ViewParser.IViewLoadOverListener viewImageLoadOver = null;
    String bgUrl = null;
    String tipUrl = null;
    boolean isBigTip = false;
    View bgView;

    public HallItemView(Context context) {
        super(context);
        hallItemView = this;
    }

    public void addBgGalleryLayout(List<String> bg_gallery) {
        for (int i = 0; i < bg_gallery.size(); i++) {
            LogUtils.i("", "sunny addBgGallery:" + bg_gallery.get(i));
        }
    }

    public void addBgLayout(String bgUri) {
        this.bgUrl = bgUri;
        BackgroundFactory bgFactory = new BackgroundFactory(mContext);
        bgComponet = (BackgroundComponent) bgFactory.createComponent(BackgroundFactory.TYPE.RECOMMEND.toString());
        bgComponet.setData(bgUri);
        bgComponet.setImageSetOverLisenter(imageSetOverListener);
        bgView = bgComponet.getView();
        addView(bgView);
    }

    public String getTitle() {
        return title;
    }

    public void addTitleLayout(String titleUri) {
        this.title = titleUri;
//        TextView tv = new TextView(mContext);
//        LayoutParams params =  new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//        params.leftMargin = 30;
//        params.topMargin = 200;
//        tv.setText(titleUri);
//        tv.setTextColor(Color.BLUE);
//        addView(tv, params);
    }

    public void addTipLayout(String tipUri, boolean isBigTip) {
        this.tipUrl = tipUri;
        this.isBigTip = isBigTip;
        CornerFactory tipFactory = new CornerFactory(mContext);
        tipComponet = (CornerComponent) tipFactory.createComponent(BackgroundFactory.TYPE.RECOMMEND.toString());
        tipComponet.setData(tipUri);
        View tipView = tipComponet.getView();

        LayoutParams params;
        if (isBigTip) {
            params = new LayoutParams(CoocaaApplication.Div(107), CoocaaApplication.Div(68));
            params.leftMargin = CoocaaApplication.Div(40);
            params.topMargin = CoocaaApplication.Div(0);
        } else {
            params = new LayoutParams(CoocaaApplication.Div(90), CoocaaApplication.Div(57));
            params.leftMargin = CoocaaApplication.Div(25);
            params.topMargin = CoocaaApplication.Div(0);
        }

        tipView.setFocusable(false);
        addView(tipView, params);
    }

    BackgroundComponent.ImageSetOverListener imageSetOverListener = new BackgroundComponent.ImageSetOverListener() {
        @Override
        public void imageSetOver(Bitmap bitmap) {
            if (viewImageLoadOver != null)
                viewImageLoadOver.viewImageLoadOver(hallItemView, bitmap);
        }
    };

    public void setIViewLoadOverListener(ViewParser.IViewLoadOverListener listener) {
        this.viewImageLoadOver = listener;
    }

    public void onDestroy() {
        viewImageLoadOver = null;
        this.removeAllViews();
        if (bgComponet != null) {
            bgComponet.destroy();
            bgComponet = null;
        }

        if (tipComponet != null) {
            tipComponet.destroy();
            tipComponet = null;
        }

        this.destroyDrawingCache();
    }

    public void onResume() {
        try {
            if (bgView == null && bgUrl != null) {
                addBgLayout(bgUrl);
            } else if (bgComponet != null && bgView != null) {
                bgComponet.onResume();
            }
        } catch (Exception e) {
            LogUtils.i("" + e);
        }
    }
}
